package com.erp.ae.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.erp.ae.domain.enums.AliExpressInterfaceEnum;
import com.erp.ae.service.IAeProductGroupService;
import com.global.iop.domain.Protocol;
import com.ruoyi.common.core.domain.R;
import com.erp.ae.utils.AeApiUtils;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商户订单Service业务层处理
 *
 * @author erp
 * @date 2023-03-09
 */
@RequiredArgsConstructor
@Service
public class AeProductGroupServiceImpl implements IAeProductGroupService {
    @Override
    public R<Object> queryStoreGroupList(Long storeId) {
        String accessToken = AeApiUtils.getToken(storeId);
        if (StringUtils.isEmpty(accessToken)) {
            return R.fail("店铺未授权");
        }
        String msg =
                AeApiUtils.sendRequest(
                        AliExpressInterfaceEnum.getProductGroups.getCode(),
                        Protocol.TOP,
                        accessToken
                );
        System.out.println(msg);
        if (msg.contains("aliexpress_product_productgroups_get_response")) {
            return R.ok(msg);
        } else {
            return R.fail("未查找到");
        }
    }

    @Override
    public R<Object> createStoreGroup(Map<String, Object> map) {
        String accessToken = AeApiUtils.getToken((Long) map.get("store_id"));
        if (StringUtils.isEmpty(accessToken)) {
            return R.fail("店铺未授权");
        }
        String msg =
                AeApiUtils.sendRequest(
                        AliExpressInterfaceEnum.createProductGroup.getCode(),
                        map,
                        Protocol.TOP,
                        accessToken
                );
        System.out.println(msg);
        if (msg.contains("aliexpress_postproduct_redefining_createproductgroup_response")) {
            return R.ok(msg);
        } else {
            return R.fail("未查找到");
        }
    }

    @Override
    public R<Object> bindingProdudtForGroup(String storeId, String groupId, String productGroups) {
        String accessToken = AeApiUtils.getToken(storeId);
        if (StringUtils.isEmpty(accessToken)) {
            return R.fail("店铺未授权");
        }
        Map<String, Object> sendMap = new HashMap<>();
        sendMap.put("group_ids", groupId);
        List<String> list = Arrays.asList(productGroups.split(","));
        Map<String, Object> productMap = new HashMap<>();
        for (String id : list) {
            sendMap.put("product_id", id);
            String msg =
                    AeApiUtils.sendRequest(
                            AliExpressInterfaceEnum.setGroups.getCode(),
                            sendMap,
                            Protocol.TOP,
                            accessToken
                    );
            System.out.println(msg);
            if (msg.contains("true")) {
                productMap.put(id, "true");
            } else {
                productMap.put(id, "false");
            }
        }
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("productMap", productMap);
        returnMap.put("groupId", groupId);
        return R.ok(JSONObject.toJSONString(returnMap));
    }

    @Override
    public R<Object> queryProductGroupIdByProductId(String storeId, List<Long> ids, String groupId) {
        String accessToken = AeApiUtils.getToken(storeId);
        Map<String, Object> returnMap = new HashMap<>();

        for (Long id : ids) {
            System.out.println(id);
            String msg =
                    AeApiUtils.sendRequest(
                            AliExpressInterfaceEnum.queryProductGroupIdByProductId.getCode(),
                            "product_id",
                            id.toString(),
                            Protocol.TOP,
                            accessToken
                    );
            JSONObject jsonObject = JSONObject.parseObject(msg);
            String targetListStr = jsonObject.getJSONObject("aliexpress_postproduct_redefining_queryproductgroupidbyproductid_response").getJSONObject("result").getString("target_list");
            if (targetListStr.contains(groupId.toString())) {
                returnMap.put(id.toString(), groupId);
            }
        }
        return R.ok(returnMap);
    }


}
